﻿using AiXiu.BLL;
using AiXiu.Common;
using AiXiu.Model;
using Reservoir.BLL;
using Reservoir.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Reservoir.WebSite
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                // 验证码校验Session["Captcha"]
                string code = txtVerification.Text;
                if (code != Session["vali"].ToString())
                {
                    this.Alert("codeerror", "输入的验证码有误");
                    return;
                }
                //登录验证
                string name = txtName.Text;
                string pwd = txtPassword.Text;
                UserBLL userBLL = new UserBLL();
                OperResult<TBUser> operResult = userBLL.LoginByName(name, pwd);
                //登录成功
                if (operResult.StatusCode != StatusCode.Succeed)
                {
                    this.Alert("loginerr", operResult.Message ?? "登录失败！");
                }
                else
                {
                    TBUser user = operResult.Result;
                    IdentityManager.SaveUser(user);
                    string returnUrl = Request.QueryString["ReturnUrl"];
                    if (!string.IsNullOrEmpty(returnUrl))
                    {
                        this.Redirect("loginsuccess", returnUrl);
                    }
                    else
                    {
                        this.Redirect("loginsuccess", "Default.aspx");
                    }
                }
            }
        }
    }
}